package com.papper.mng.controller.base;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.papper.common.bean.base.BaseConstIp;
import com.papper.common.config.PapperStorage;
import com.papper.common.constant.SystemConstants;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.DwzPage;
import com.papper.common.service.base.BaseConstIpService;
import com.papper.common.utils.tool.StringTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:IP地址库
 */
@Controller
@RequestMapping("/base/const/ip")
public class BaseConstIpController extends BaseAction {

    @Autowired
    BaseConstIpService baseConstIpService;

    /**
     * IP地址库
     *
     * @return
     */
    @RequestMapping(value = "")
    public String execute(HttpServletRequest _request, Model model) {
        // 字段查询条件
        Map<String, Object> objects = Maps.newHashMap();
        extraParams(objects, _request, BaseConstIp.class);
        modelParm(objects, model);
        // 分页查询条件
        int pageNum    = StringTool.parseInteger(_request.getParameter("pageNum"), 1);
        int numPerPage = StringTool.parseInteger(_request.getParameter("numPerPage"), SystemConstants.PAGE_LIMIT_16);
        // 数据查询组装
        Example               example = buildExample(objects, model, BaseConstIp.class);
        PageInfo<BaseConstIp> pageRtn = baseConstIpService.findPage(pageNum, numPerPage, example);
        model.addAttribute("page", new DwzPage(pageNum, numPerPage, pageRtn.getTotal()));
        model.addAttribute("records", pageRtn.getList());
        return render("base/const/ip/index");
    }

    /**
     * IP地址库_新增
     */
    @GetMapping(value = "/add")
    public String add_get() {
        return render("base/const/ip/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, BaseConstIp baseConstIp) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        // 添加IP地址库
        try {
            baseConstIp.setDataStatus(DataStatusEnum.OPEN.getCode());
            baseConstIp.setCreateTime(new Date());
            baseConstIp.setModifyTime(new Date());
            baseConstIpService.save(baseConstIp);
            return new DwzResponse("200", "IP地址库添加成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "IP地址库添加失败:" + e.getMessage() + "!!!");
        }
    }

    /**
     * IP地址库_更新
     */
    @GetMapping(value = "/edit")
    public String edti_get(Model model, Long id) {
        BaseConstIp entity = baseConstIpService.findById(id);
        model.addAttribute("entity", entity);
        return render("base/const/ip/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, BaseConstIp baseConstIp) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (baseConstIp == null || baseConstIp.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            baseConstIp.setModifyTime(new Date());
            baseConstIpService.update(baseConstIp);
            return new DwzResponse("200", "IP地址库修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "IP地址库修改失败:" + e.getMessage());
        }
    }

    /**
     * IP地址库_删除
     *
     * @param id
     * @
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del_post(HttpServletRequest _request, Long id) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数ID为空！");
        }

        BaseConstIp old = baseConstIpService.findById(id);

        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        try {
            baseConstIpService.deleteById(id);
            return new DwzResponse("200", "IP地址库删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "IP地址库删除失败:" + e.getMessage());
        }
    }

    /**
     * IP地址库_详情
     *
     * @return
     */
    @GetMapping(value = "/detail")
    public String detail(HttpServletResponse _response, Model model, Long id) {
        BaseConstIp baseConstIp = baseConstIpService.findById(id);
        model.addAttribute("entity", baseConstIp);
        return render("base/const/ip/detail");
    }

    /**
     * IP地址库_重处理
     *
     * @param id
     * @throws IOException
     */
    @PostMapping(value = "/redo")
    @ResponseBody
    public DwzResponse redo_post(HttpServletRequest _request, Long id) throws IOException {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }

        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数[id]为空！");
        }
        BaseConstIp old = baseConstIpService.findById(id);
        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        boolean ipFlg = getIpFromAliyun(old);
        if (ipFlg) {
            return new DwzResponse("200", "系统IP重获取成功!!!");
        } else {
            return new DwzResponse("300", "系统IP重获取失败!!!");
        }
    }

    private boolean getIpFromAliyun(BaseConstIp constIp) {
        String aliyunIpCode = PapperStorage.Config("SysAliyunConfig", "ip.aliyun.appcode", null);
        if (aliyunIpCode == null) {
            return false;
        }
        String ipResp = null;
        //TODO-远程IP信息获取
        return false;
    }

}



